!Espacement des bandes de précipitation et leur largeur
module fonctions
implicit none
!-----------------------------------
character*(*), parameter :: filenamein = 'mb-E.in'
character*(*), parameter :: filenameout = 'liesedata.txt'
real, parameter :: eps = 0.12 !définir la bande comme 1-eps
end module fonctions

!========================================================================

program liesedata
use fonctions
implicit none
real, allocatable, dimension(:,:) :: band,bandtemp !n,xn(centre de la bande),wn(largeur de la bande)
integer :: length,i,j,jmin,jmax,n
real :: sigma,x

open(unit=1,file=filenamein,status='OLD')
read(1,1001) length !read the parameters, and keep only sigma and length
read(1,1001) i
read(1,1001) i
read(1,1000) sigma
read(1,1000) sigma
read(1,1000) sigma
read(1,1000) sigma
read(1,1000) sigma
read(1,1000) sigma
read(1,1000) sigma
1000 FORMAT(25X,F20.4)
1001 FORMAT(25X,I20)

allocate(bandtemp(1:length,1:2)) !read the data
n=0
i=0
readfile: do
	i=i+1
	if (i==length) exit readfile
	read(1,*) j,x
	if (x>=1.-eps) then
		jmin=j
		endofband: do
			i=i+1
			if (i==length) exit readfile
			read(1,*) j,x
			if (x<1.-eps) then
				jmax=j-1
				exit endofband
			end if
		end do endofband
		n=n+1
		bandtemp(n,1)=real(jmax+jmin)/2.
		bandtemp(n,2)=real(jmax-jmin)
	end if
end do readfile
close(1)

allocate(band(1:n,1:2)) !keep the n values
band(1:n,:)=bandtemp(1:n,:)
deallocate(bandtemp)

open(unit=1,file=filenameout) !save the (n-1) values: n, x_n, w_n, x_{n+1}/x_n, w_{n+1}/w_n
do i=1,n-1
	write(1,*) i,band(i,1),band(i,2),band(i+1,1)/band(i,1),band(i+1,2)/band(i,2)
end do
close(1)

deallocate(band)
end program liesedata